package com.starsoft.frame.base.service;

import java.util.List;

import org.springframework.stereotype.Service;

import com.starsoft.frame.base.bean.BaseMenu;
import com.starsoft.frame.base.bean.BaseRole;
import com.starsoft.frame.base.bean.BaseUser;
import com.starsoft.frame.core.service.BaseService;

@Service
public class MenuService extends BaseService{
	
	/**
	 * 根据用户角色，查询菜单
	 * @param user
	 * @return
	 */
	public List<BaseMenu> getAllMenu(BaseUser user){
		BaseRole br=null;
		List<BaseMenu> re=null;
		if(user.getRoleId()!=null){
			br=hibernateDao.get(BaseRole.class, user.getRoleId());
		}
		if(br==null){
			return re;
		}
		
		if("sa".equals(br.getRoleCode())){
			re=hibernateDao.getAll(BaseMenu.class);
		}else{
			re=hibernateDao.find("from BaseMenu where code in " +
					"(select menuCode from BaseRolemenu where roleId=?0)",
					br.getRoleId());
		}
		return re;
	}

	public void saveOrUpdate(BaseMenu menu) {
		hibernateDao.saveOrUpdate(menu);
	}

	public void delete(BaseMenu menu) {
		hibernateDao.delete(menu);
	}

	public BaseMenu get(String code) {
		return hibernateDao.get(BaseMenu.class, code);
	}
}
